package com.iocup.keybastion.core.profile;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

@Getter
@Setter
public class CommonUserProfile implements UserProfile {

    private Object id;

    private Map<String, Object> attributes = new HashMap<>();

    private boolean isRemembered = false;

    private Set<String> roles = new HashSet<>();

    private Set<String> permissions = new HashSet<>();

    private String device;

    private String loginName;

    private String username;

    @Override
    public <T> T getId() {
        return (T) id;
    }

    @Override
    public <T> UserProfile setId(T id) {
        this.id = id;
        return this;
    }

    @Override
    public UserProfile setLoginName(String loginName) {
        this.loginName = loginName;
        return this;
    }

    @Override
    public UserProfile setUsername(String username) {
        this.username = username;
        return this;
    }

    @Override
    public Object getAttribute(String name) {

        return this.attributes.get(name);
    }

    @Override
    public boolean containsAttribute(String name) {
        return this.attributes.containsKey(name);
    }

    @Override
    public UserProfile setAttribute(Map<String, Object> data) {
        if(data!=null&& !data.isEmpty()){
            this.attributes.putAll(data);
        }

        return this;
    }

    @Override
    public UserProfile addAttribute(String key, Object value) {
        if(StringUtils.isNotEmpty(key)&& value!=null){
            this.attributes.put(key, value);
        }

        return this;
    }

    @Override
    public UserProfile addRole(String role) {
        if(StringUtils.isNotBlank(role))
        this.roles.add(role);
        return this;
    }

    @Override
    public UserProfile addRoles(Collection<String> roles) {
        if(CollectionUtils.isNotEmpty(roles))
        this.roles.addAll(roles);
        return this;
    }

    @Override
    public UserProfile setRoles(Collection<String> roles) {
        this.roles = new HashSet<>();
        return addRoles(roles);
    }

    @Override
    public UserProfile addPermission(String permission) {
        if(StringUtils.isNotBlank(permission))
        this.permissions.add(permission);
        return this;
    }

    @Override
    public UserProfile addPermissions(Collection<String> permissions) {
        if(CollectionUtils.isNotEmpty(permissions))
        this.permissions.addAll(permissions);
        return this;
    }

    @Override
    public UserProfile setPermissions(Collection<String> permissions) {
        this.permissions = new HashSet<>();
        return addPermissions(permissions);
    }

    @Override
    public UserProfile setRemembered(boolean rme) {
        this.isRemembered = rme;
        return this;
    }


    @Override
    public UserProfile setDevice(String device) {
        this.device = device;
        return this;
    }


    /**
     * 保存用户信息时会清除掉一些敏感的认证信息
     */
    public void removeLoginData() {

    }
}
